import org.json.JSONObject;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/get_book")
public class GetBook extends HttpServlet {
    private static final long serialVersionUID = 1L;

    
    private static final String URL = "jdbc:mysql://localhost:3306/linux_exam";
    private static final String USER = "root";
    private static final String PASSWORD = "1qaz@WSX#EDC";

    
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
       
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();

        
        String bookNumber = request.getParameter("number");

        if (bookNumber == null || bookNumber.isEmpty()) {
           
             JSONObject errorJson = new JSONObject();
            errorJson.put("error", "图书编号不能为空");
            out.println(errorJson.toString());
            out.close();
            return;
        }

        JSONObject resultJson = getBook(bookNumber);
        if (resultJson!= null) {
            out.println(resultJson.toString());
        } else {
            JSONObject errorJson = new JSONObject();
            errorJson.put("error", "未查询到指定图书信息");
            out.println(errorJson.toString());
        }

        out.close();
    }

   
     public JSONObject getBook(String bookNumber) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            
            Class.forName("com.mysql.cj.jdbc.Driver");
            
             connection = DriverManager.getConnection(URL, USER, PASSWORD);

            
             String sql = "SELECT * FROM t_book WHERE number =?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, bookNumber);

            
             resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                JSONObject bookInfo = new JSONObject();
                bookInfo.put("number", resultSet.getString("number"));
                bookInfo.put("name", resultSet.getString("name"));
                bookInfo.put("author", resultSet.getString("author"));
                 return bookInfo;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            
            try {
                if (resultSet!= null) resultSet.close();
                if (preparedStatement!= null) preparedStatement.close();
                if (connection!= null) connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
